package com.tf_cut.admin.platform.controller;

import com.alibaba.fastjson.JSON;
import com.tf_cut.admin.backend.api.BackEndApi;
import com.tf_cut.admin.common.pojo.PageInfo;
import com.tf_cut.admin.common.pojo.Result;
import com.tf_cut.admin.platform.excel.GrantPersonExcel;
import com.tf_cut.admin.platform.excel.RechargeExcel;
import com.tf_cut.admin.platform.vo.GrantDetailVo;
import com.tf_cut.admin.platform.vo.GrantVo;
import com.tf_cut.admin.platform.vo.RechargeVo;
import com.tf_cut.admin.seller.vo.SellerOrderVo;
import com.tf_cut.admin.sys.sysuser.service.SysUserService;
import com.tf_cut.admin.sys.sysuser.vo.SysUserVo;
import com.tf_cut.admin.util.ExcelUtils;
import com.tf_cut.admin.util.SecurityUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;


@RestController
@RequestMapping("/platform/recharge")
@Slf4j
public class RechargeController {

	private static final String FILE_NAME = "充值记录";
    @Autowired
    private BackEndApi platformRechargeApi;
    @Autowired
    private SysUserService sysUserService;

    @GetMapping("list")
    public ModelAndView sellerList() {
        return new ModelAndView("platform/recharge/list", "platformRecharge", null);
    }

    @PostMapping(value = "list/page")
    public Result<PageInfo<RechargeVo>> queryRechargeList(RechargeVo rechargeVo) {
        String json = JSON.toJSONString(rechargeVo);
        json = platformRechargeApi.queryPlatformRechargeList(json);
        return Result.of(JSON.parseObject(json, PageInfo.class));
    }

    @PostMapping("update")
    public String updateRecharge(RechargeVo avo) {
        String rtnJson = "";
        if (StringUtils.isNoneBlank(avo.getStatus()) && StringUtils.isNoneBlank(avo.getId())) {
            try {
                SysUserVo user = sysUserService.findByLoginName(SecurityUtil.getLoginUser().getUsername()).getData();

                avo.setMerchantNumber(user.getMerchantNumber());
                avo.setPlatform(user.getPlatform());

                rtnJson = platformRechargeApi.updateRecharge(JSON.toJSONString(avo));
            } catch (Exception e) {
                log.error("updateRecharge error:{}",e.getMessage(), e);
            }
        }
        return rtnJson;
    }
    
    
    @GetMapping(value = "export")
    public Result<PageInfo<RechargeVo>> queryRechargeExportList(@RequestParam("sellerName") String sellerName, HttpServletResponse response) {
        try{
            List<RechargeVo> list = platformRechargeApi.queryRechargeExportList(sellerName);
            for(int i=0;i<list.size();i++){
            	if(list.get(i).getStatus().equals("0")){
            		list.get(i).setStatus("审核中");
            	}else if(list.get(i).getStatus().equals("1")){
            		list.get(i).setStatus("已充值");
            	}else if(list.get(i).getStatus().equals("2")){
            		list.get(i).setStatus("驳回");
            	}else{
            		list.get(i).setStatus("无");
            	}
            }
            ExcelUtils.exportExcelToTarget(response, FILE_NAME, list, RechargeExcel.class);
            return Result.of(JSON.parseObject("", PageInfo.class));
        }catch (Exception e){
            log.error("exportGrantList error:{}",e.getMessage(),e);
        }
        return Result.of(JSON.parseObject("", PageInfo.class));
    }
    
    
    
    @RequestMapping("/queryByMoney")
    public Result<List<RechargeVo>> queryByMoney(@RequestParam("sellerName") String sellerName ) {
    	List<RechargeVo> list = platformRechargeApi.queryByMoney(sellerName);
    	 return Result.of(list);
    }
}
